package Tools;

import javax.swing.JFrame;

import FileManager.SimulateContent;
import FileManager.SimulationDirectory;
import FileManager.SimulationFile;
import FileMangerClient.MyComputerController;
import javafx.event.EventHandler;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import Tools.Wordwin;

public class AddSomething {
	
	 public void CreateVBox(String URL,SimulationDirectory simulationDirectory,MyComputerController myComputerController) {
	    	VBox vBox = new VBox();
	    	Image Menu = new Image(URL);
	        ImageView MenuView = new ImageView(Menu);
	        MenuView.setFitHeight(70);
	        MenuView.setFitWidth(70);
	        Label label = new Label( "     "+simulationDirectory.getName());
	        label.setContentDisplay(ContentDisplay.CENTER);
	        vBox.setPrefHeight(100);
	        vBox.getChildren().addAll(MenuView,label);  
	        vBox.setStyle("-fx-background-color:#FAFAFA"); 
	        vBox.setOnMouseClicked(new EventHandler<MouseEvent>() {
	            @Override
	            public void handle(MouseEvent event)
	            {
	            	
	                if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2)//双击打开文件夹
	                {
	                	System.out.println("双击生效");
	                	myComputerController.setCurrent_folder(simulationDirectory);
		            	 myComputerController.setSelectedItem(simulationDirectory.getTreeItem());
		            	 
		                 for(SimulationDirectory directory: myComputerController.simulationDirectorys)
		                 {
		                	    System.out.println(directory.getName());
		                	    System.out.println(simulationDirectory.getTreeItem().getValue()); 
		                 		System.out.println(myComputerController.selectedItem.getValue()); 
		                 	if(directory.getName().equals(myComputerController.selectedItem.getValue())) {
		                 		
		                 		myComputerController.update(simulationDirectory);
		                 		myComputerController.path.setText(directory.getAbsolute());
		                 		break;
		                 	}
		                 }
	                }
	                else if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1)  //单击
	                {
	                	//选中文件夹
	                	myComputerController.setSelect(true);
	                	myComputerController.setSelectType(false);
	                	myComputerController.Current_File= new SimulationFile();
	                	myComputerController.Current_VBOX.setStyle("-fx-background-color:#FAFAFA");
	                	myComputerController.setCurrent_VBOX(vBox);
	               		myComputerController.setCurrent_Diretory(simulationDirectory); 
	               		myComputerController.Current_VBOX.setStyle("-fx-background-color:#66ccff");
	               	 System.out.println(myComputerController.Current_Diretory.getName());   
	                	
	                  }
	                else if(event.getButton()==MouseButton.SECONDARY)  //右键
	                {
	                	//选中文件或文件夹
	                	myComputerController.setSelect(true);
	                	myComputerController.setSelectType(false);
	                	myComputerController.Current_File= new SimulationFile();
	                	myComputerController.Current_VBOX.setStyle("-fx-background-color:#FAFAFA");
	                	myComputerController.setCurrent_VBOX(vBox);
	               		myComputerController.setCurrent_Diretory(simulationDirectory); 
	               		myComputerController.Current_VBOX.setStyle("-fx-background-color:#66ccff");
	               	 System.out.println(myComputerController.Current_Diretory.getName());   
	                	
	                }
	              
	              }
	           
	        });
	        
	        
	       
	        simulationDirectory.setvBox(vBox);
	       
	        
	        
	        myComputerController.getFlowPane().getChildren().add(vBox);
	    }
	// 添加文件
	 public void CreateVBox(String URL,SimulationFile simulationFile,MyComputerController myComputerController) {
	    	VBox vBox = new VBox();
	    	Image Menu = new Image(URL);
	        ImageView MenuView = new ImageView(Menu);
	        MenuView.setFitHeight(70);
	        MenuView.setFitWidth(70);
	        Label label = new Label( "     "+simulationFile.getName()+"."+simulationFile.getAttribute());
	        vBox.setPrefHeight(100);
	        vBox.getChildren().addAll(MenuView,label);  
	        vBox.setStyle("-fx-background-color:#FAFAFA");
	        vBox.setOnMouseClicked(new EventHandler<MouseEvent>() {

	            @Override
	            public void handle(MouseEvent event)
	            {
	            	
	                if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2)//双击打开文件
	                {
	                	if(simulationFile.getAttribute().equals("T"))  //双击文本文件打开编辑器
	                	{
	                	 JFrame wordwin=new Wordwin(simulationFile,myComputerController.getDisk());   
	                     wordwin.setVisible(true);
	                	}
	                	else    //双击可执行文件执行
	                	{
	                		excuteFile(simulationFile.getContent(),myComputerController);
	                	}
	                }
	                else if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1)  //单击
	                {
	                	myComputerController.setSelect(true);
	                	myComputerController.setSelectType(true);
	         	       
	                	myComputerController.Current_VBOX.setStyle("-fx-background-color:#FAFAFA");
	                	myComputerController.setCurrent_VBOX(vBox);
	               		myComputerController.setCurrent_File(simulationFile);
	               		myComputerController.Current_VBOX.setStyle("-fx-background-color:#66ccff");
	               	 System.out.println(myComputerController.Current_File.getName());   
	                	
	                  }
	                else if(event.getButton()==MouseButton.SECONDARY)  //右键
	                {
	                	//选中文件或文件夹
	                	myComputerController.setSelect(true);
	                	myComputerController.setSelectType(true);
		         	       
	                	myComputerController.Current_VBOX.setStyle("-fx-background-color:#FAFAFA");
	                	myComputerController.setCurrent_VBOX(vBox);
	               		myComputerController.setCurrent_File(simulationFile);
	               		myComputerController.Current_VBOX.setStyle("-fx-background-color:#66ccff");
	               	 System.out.println(myComputerController.Current_File.getName());   
	                }  
	                System.out.println(myComputerController.Current_File.getName());
	             }
	        });
	  
	        simulationFile.setvBox(vBox);
	       
	       
	        myComputerController.getFlowPane().getChildren().add(vBox);
	    }
	 //添加目录项
	 public void AddTreeItem(SimulateContent simulationContent,SimulationDirectory Father,FlowPane FileMenuPane) {
		 
		   Father.addChildren(simulationContent, simulationContent.getTreeItem());
		   Father.getTreeItem().getChildren().add(simulationContent.getTreeItem());
	}
	//添加搜索结果项
	 //文件夹
	 public void AddSearchResult(String URL,SimulationDirectory simulationDirectory,MyComputerController myComputerController) {
		    HBox hBox = new HBox();
	    	Image Menu = new Image(URL);
	        ImageView MenuView = new ImageView(Menu);
	        MenuView.setFitHeight(70);
	        MenuView.setFitWidth(70);
	        Label label = new Label( simulationDirectory.getName()+":"+simulationDirectory.getAbsolute());
	        label.setContentDisplay(ContentDisplay.CENTER);
	        hBox.setPrefWidth(650);
	        hBox.setPrefHeight(100);
	        hBox.getChildren().addAll(MenuView,label);  
	        hBox.setStyle("-fx-background-color:#FAFAFA"); 
	        hBox.setOnMouseClicked(new EventHandler<MouseEvent>() {
	            @Override
	            public void handle(MouseEvent event)
	            {
	            	
	                if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2)//双击打开文件夹
	                {
	                	System.out.println("双击生效");
	                	myComputerController.setCurrent_folder(simulationDirectory);
		            	 myComputerController.selectedItem = simulationDirectory.getTreeItem();
		                 for(SimulationDirectory directory: myComputerController.simulationDirectorys)
		                 {
		                 	if(directory.getName().equals( myComputerController.selectedItem.getValue())) {
		                 		myComputerController.update(simulationDirectory);
		                 		myComputerController.path.setText(directory.getAbsolute());
		                 		
		                 	}
		                 }
	                }
	                else if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1)  //单击
	                {
	                	//选中文件夹
	                	myComputerController.setSelect(true);
	                	myComputerController.setSelectType(false);
	                	myComputerController.Current_File= new SimulationFile();
	                	myComputerController.Current_HBOX.setStyle("-fx-background-color:#FAFAFA");
	                	myComputerController.setCurrent_HBOX(hBox);
	               		myComputerController.setCurrent_Diretory(simulationDirectory); 
	               		myComputerController.Current_HBOX.setStyle("-fx-background-color:#66ccff");
	               	 System.out.println(myComputerController.Current_Diretory.getName());   
	                	
	                  }
	                else if(event.getButton()==MouseButton.SECONDARY)  //右键
	                {
	                	//选中文件或文件夹
	                	myComputerController.setSelect(true);
	                	myComputerController.setSelectType(false);
	                	myComputerController.Current_File= new SimulationFile();
	                	myComputerController.Current_HBOX.setStyle("-fx-background-color:#FAFAFA");
	                	myComputerController.setCurrent_HBOX(hBox);
	               		myComputerController.setCurrent_Diretory(simulationDirectory); 
	               		myComputerController.Current_HBOX.setStyle("-fx-background-color:#66ccff");
	               	 System.out.println(myComputerController.Current_Diretory.getName());   
	                	
	                }
	              
	              }
	           
	        });
	        
	        
	        
	        
	        myComputerController.getFlowPane().getChildren().add(hBox);
	 }
	 
	 public void AddSearchResult(String URL,SimulationFile simulationFile,MyComputerController myComputerController) {
		    HBox hBox = new HBox();
	    	Image Menu = new Image(URL);
	        ImageView MenuView = new ImageView(Menu);
	        MenuView.setFitHeight(70);
	        MenuView.setFitWidth(70);
	        Label label = new Label( simulationFile.getName()+":"+simulationFile.getAbsolute());
	        label.setContentDisplay(ContentDisplay.CENTER);
	        hBox.setPrefWidth(650);
	        hBox.setPrefHeight(100);
	        hBox.getChildren().addAll(MenuView,label);  
	        hBox.setStyle("-fx-background-color:#FAFAFA"); 
	        hBox.setOnMouseClicked(new EventHandler<MouseEvent>() {
	        @Override
            public void handle(MouseEvent event)
            {
            	
                if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2)//双击打开文件
                {
                	if(simulationFile.getAttribute().equals("T"))  //双击文本文件打开编辑器
                	{
                	 JFrame wordwin=new Wordwin(simulationFile,myComputerController.getDisk());   
                     wordwin.setVisible(true);
                	}
                	else    //双击可执行文件执行
                	{
                		excuteFile(simulationFile.getContent(),myComputerController);
                		
                	}
                }
                else if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1)  //单击
                {
                	myComputerController.setSelect(true);
                	myComputerController.setSelectType(true);
         	        
                	myComputerController.Current_HBOX.setStyle("-fx-background-color:#FAFAFA");
                	myComputerController.setCurrent_HBOX(hBox);
               		myComputerController.setCurrent_File(simulationFile);
               		myComputerController.Current_HBOX.setStyle("-fx-background-color:#66ccff");
               	 System.out.println(myComputerController.Current_File.getName());   
                	
                  }
                else if(event.getButton()==MouseButton.SECONDARY)  //右键
                {
                	//选中文件或文件夹
                	myComputerController.setSelect(true);
                	myComputerController.setSelectType(true);
	         	       
                	myComputerController.Current_HBOX.setStyle("-fx-background-color:#FAFAFA");
                	myComputerController.setCurrent_HBOX(hBox);
               		myComputerController.setCurrent_File(simulationFile);
               		myComputerController.Current_HBOX.setStyle("-fx-background-color:#66ccff");
               	 System.out.println(myComputerController.Current_File.getName());   
                }  
                System.out.println(myComputerController.Current_File.getName());
             }
        });
	      
	        
	        myComputerController.getFlowPane().getChildren().add(hBox);
	 }
	 private void excuteFile(String str,MyComputerController myComputerController) {
		 if(process.CPU.getCpu().create(str))
		 myComputerController.result.setText("创建进程成功");
		 else {
			 myComputerController.result.setText("内存空间不足，创建进程失败");
		}
		 
	 }
}
